package com.cheng.service.system.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cheng.dao.system.RoleDao;
import com.cheng.domain.system.Menu;
import com.cheng.domain.system.Permission;
import com.cheng.domain.system.Role;
import com.cheng.domain.system.User;
import com.cheng.service.system.RoleService;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleDao roleDao;

	@Override
	public List<Role> findByUser(User user) {
		return roleDao.findByUserId(user.getId());
	}

	@Override
	public List<Role> findAll() {
		return roleDao.findAll();
	}

	@Override
	public void saveRole(Role model, Integer[] permissionids, String menuIds) {
		if (StringUtils.isNotBlank(menuIds)) {
			Menu menu = null;
			String[] str = menuIds.split(",");
			for (String ids : str) {
				menu = new Menu();
				menu.setId(Integer.parseInt(ids));
				model.getMenus().add(menu);
			}
		}
		if (permissionids != null) {
			Permission permission = null;
			for (Integer pid : permissionids) {
				permission = new Permission();
				permission.setId(pid);
				model.getPermissions().add(permission);
			}
		}
		roleDao.save(model);
	}

}
